﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.IO;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class SkinController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public SkinController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void SkinList()
        {
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            var condition = String.Empty;
            DataPage<Skin> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            Int32 lockin = ctx.GetInt("lockin");
            switch (lockin)
            {
                case 1:
                    condition += " lockin = 1 ";
                    break;

                case 2:
                    condition += " lockin = 0 ";
                    break;
            }
            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "desc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = Skin.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void SkinAdd()
        {
            SetPublic();
            target(SkinAddDo);
            set("load", to(new AdminController().Load) + "?");
            Configs config = Configs.find("valname=:v").set("v", "is_inputclose").first();
            set("refalse", config.value);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("tab", ctx.Get("tab") == null || ctx.Get("tab").IsNullOrEmpty() ? "false" : "true");
            set("checkcode", to(CheckCode));
        }

        [HttpPost]
        public virtual void SkinAddDo()
        {
            Skin s = new Skin();
            s.name = ctx.Post("name");
            s.code = ctx.Post("code");
            s.lockin = 0;
            s.isclass = 0;
            s.addtime = DateTime.Now;
            Result result = s.insert();
            if (result.HasErrors)
            {
                ctx.web.ResponseWrite("false");
                logService.Add((User)ctx.viewer.obj, lang("skinmain_add_log") + lang("log_extra_no"), ctx.Ip);
            }
            else
            {
                var folder = strUtil.Join(strUtil.Join(sys.Path.Root, MvcConfig.Instance.ViewDir + "templates"), s.code);
                if (!FileSystemObject.IsExist(PathHelper.Map(folder), FsoMethod.Folder))
                {
                    FileSystemObject.Create(folder, FsoMethod.Folder);
                }
                ctx.web.ResponseWrite("true");
                logService.Add((User)ctx.viewer.obj, lang("skinmain_add_log") + lang("log_extra_ok"), "{Id:" + s.Id + ", Name:'" + s.name + "'}", "", ctx.Ip);
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void BakSkin(Int64 id)
        {
            Skin s = Skin.findById(id);
            var folder = strUtil.Join(strUtil.Join(sys.Path.Root, MvcConfig.Instance.ViewDir + "templates"), s.code);
            if (!FileSystemObject.IsExist(PathHelper.Map(folder), FsoMethod.Folder))
            {
                ctx.web.ResponseWrite("false");
                ctx.web.ResponseEnd();
            }
            else
            {
                var folder1 = strUtil.Join(sys.Path.Root, "App_Data/backup/" + s.code);
                FileSystemObject.CopyDirectory(PathHelper.Map(folder), PathHelper.Map(folder1));
                ctx.web.ResponseWrite("true");
                ctx.web.ResponseEnd();
            }

        }

        [HttpGet]
        public virtual void DelSkin(Int64 id)
        {
            var isclass = ctx.PostInt("isclass");
            if (isclass == 1)
            {
                ctx.web.ResponseWrite("false");
                ctx.web.ResponseEnd();
            }
            Skin s = Skin.findById(id);
            var code = s.code;
            if (s.delete() > 0)
            {
                var folder = strUtil.Join(strUtil.Join(sys.Path.Root, MvcConfig.Instance.ViewDir + "templates"), code);
                FileSystemObject.Delete(folder, FsoMethod.Folder);
                ctx.web.ResponseWrite("true");
                logService.Add((User)ctx.viewer.obj, lang("skinmain_del_log") + lang("log_extra_ok"), "{Id:" + id + "}", "", ctx.Ip);
            }
            else
            {
                ctx.web.ResponseWrite("false");
                logService.Add((User)ctx.viewer.obj, lang("skinmain_del_log") + lang("log_extra_no"), ctx.Ip);
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void Setting(Int64 id)
        {
            var code = ctx.Get("code");
            var folder = strUtil.Join(strUtil.Join(sys.Path.Root, MvcConfig.Instance.ViewDir + "templates"), code);
            if (!FileSystemObject.IsExist(PathHelper.Map(folder), FsoMethod.Folder))
            {
                ctx.web.ResponseWrite("false");
                ctx.web.ResponseEnd();
            }
            else
            {
                var dt = FileSystemObject.GetDirectoryInfos(folder, FsoMethod.All);
                if (dt.Rows.Count == 0)
                {
                    ctx.web.ResponseWrite("false");
                    ctx.web.ResponseEnd();
                }
                else
                {
                    Skin.updateBatch("isclass = 0", "");
                    Skin.updateBatch("isclass = 1", "id =" + id);
                    logService.Add((User)ctx.viewer.obj, lang("skinmain_class_log") + lang("log_extra_ok"), "{Id:" + id + ", 主题标识:'" + code + "'}", "", ctx.Ip);
                    ctx.web.ResponseWrite("true");
                    ctx.web.ResponseEnd();
                }
            }
        }

        [HttpPost]
        public virtual void CheckCode()
        {
            var count = Skin.count(" code = '" + ctx.Post("code") + "'");
            if (count > 0)
            {
                ctx.web.ResponseWrite("false");
            }
            else
            {
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

    }
}
